#!/usr/bin/env python

import numpy as np
from PyQt4 import QtCore
from dateutil import parser

floatTypes = (float,np.float64)
intTypes = (int,np.int32)
#strTypes = (str, QtCore.QString)#BHC
strTypes = (str, str)#BHC




class generalVarModel(QtCore.QAbstractTableModel):
    """
    Specific model for single variable of floats, ints, and strings.
    """
    def __init__(self,parent=None):
        return QtCore.QAbstractTableModel.\
            __init__(self,parent)

    def __readVarData__(self,index):
        #print "__readVarData__ called"
        return self.__theVar#BHC
        
    def __setVarData__(self,index,value):
        #print "__setVarData__ called"
        print "setVarData type", type(value)
        self.__theVar = value#BHC
        print "theVar: ", self.__theVar

    def __writeOnCell__(self,index):
        #print "__writeOnCell__ called"
        if self.__theDataType in floatTypes:
            return QtCore.QVariant("%.6f" % self.__readVarData__(index))
        elif self.__theDataType in intTypes:
            return QtCore.QVariant("%d" % self.__readVarData__(index))
        elif self.__theDataType in strTypes:#BHC
            return QtCore.QVariant("%s" % self.__readVarData__(index))#BHC
        else:
            date = self.__readVarData__(index)
            return QtCore.QVariant(date.strftime("%d/%m/%Y"))
    
    def __dataConversion__(self,value):
        print "__dataConversion__ called"
        if self.__theDataType in floatTypes:
            print type(value)
            print value.toDouble()
            return value.toDouble()#changed to Double()
        elif self.__theDataType in intTypes:
            print type(value)
            return value.toInt()
        elif self.__theDataType in strTypes:#BHC
            print type(value)
            return value.toString()#BHC
        else:
            return self.__toDate__(value)

    def __toDate__(self,value):
        #print "__toDate__ Called"
        try:
            date = parser.parse(value.toString().__str__(),dayfirst=True).date()
            return date, True
        except:
            return 0, False

    def setTheVar(self,aVar):
        self.__theVar = aVar
        #self.__theShape = len(np.shape(aList))
        #self.__theDataType = None
        self.__theDataType = type(aVar)#BHC
        # Check if list dimension is not greater thant nxn
        #if self.__theShape > 2:
        #    raise Exception("List cannot be visualized\nbecause of its dimensionality.")
        # Check the list's element type
        #if self.__theShape == 2:
        #    self.__theDataType = type(aList[0][0])
        #elif self.__theShape == 0:#BHC
            #print "__theShape is 0"
        #self.__theDataType = type(aVar)#BHC
        #else:
        #    self.__theDataType = type(aList[0])
    
    def rowCount(self,index):
        #print "rowCount Called"
        #if self.__theShape > 0:#BHC
        #    return len(self.__theVar)#BHC
        #else:#BHC
        return 1
    
    def columnCount(self,index):
        #print "columnCount Called"
        #if self.__theShape == 2:
        #    return len(self.__theVar[0])
        #else:
        return 1
    
    def headerData(self,section,orientation,role):
        #print "headerData Called"
        if role == QtCore.Qt.DisplayRole:
            return QtCore.QVariant(section)
        return QtCore.QVariant()

    def flags(self,index):
        #print "flags Called"
        if not index.isValid():
            return QtCore.Qt.ItemIsEnabled
        flags = QtCore.QAbstractItemModel.flags(self,index)
        return flags | QtCore.Qt.ItemIsEditable
    
    def data(self,index,role):
        #print "data Called"
        if role == QtCore.Qt.TextAlignmentRole:
            return QtCore.QVariant(QtCore.Qt.AlignRight | QtCore.Qt.AlignVCenter)
        elif role in (QtCore.Qt.DisplayRole, QtCore.Qt.EditRole)\
            and index.isValid():
            return self.__writeOnCell__(index)
        return QtCore.QVariant()

    def setData(self,index,value,role):
        self.__theVar = self.__dataConversion__(value)
        #self.__theVar = value
        return True
        #print "setData Called"
        #if index.isValid() and role == QtCore.Qt.EditRole:
        #    value, conversionIsOk = self.__dataConversion__(value)
        #    print "Conversion OK?: ", conversionIsOk
        #    #if conversionIsOk: self.__setVarData__(index,value)
        #    if conversionIsOk: self.__theVar = value
        #    return True
        #return False
